package dome2;

import java.util.Stack;

public class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(tokens[i].equals("*")||tokens[i].equals("/")||tokens[i].equals("+")||tokens[i].equals("-")) {
                int val2 = stack.pop();
                int val1 = stack.pop();
                switch (tokens[i]) {
                    case "*" :
                        stack.push(val1 * val2);
                        break;
                    case "/" :
                        stack.push(val1 / val2);
                        break;
                    case "+" :
                        stack.push(val1 + val2);
                        break;
                    case "-" :
                        stack.push(val1 - val2);
                        break;
                }
            }else {
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.pop();
    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param pushV int整型一维数组
     * @param popV int整型一维数组
     * @return bool布尔型
     */
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && j<pushV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[' ) {
                stack.push(ch);
            }else {
                if(stack.empty()) {
                    return false;
                }
                char topL = stack.peek();
                if(topL == '(' && ch == ')' ||
                   topL == '{' && ch == '}' ||
                   topL == '[' &&ch == ']') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }

}
